package com.car.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.car.entity.Citytable;
import com.car.entity.Provincialtable;
import com.car.entity.Storetable;
import com.car.entity.Vehiclemotioncar;
import com.car.service.CardetailstableService;
import com.car.service.CitytableService;
import com.car.service.ProvincialtableService;
import com.car.service.StoretableService;
import com.car.service.VehiclebrandService;
import com.car.service.VehiclemotioncarService;
import com.car.util.Random;
import com.car.vo.CardetailstableVo;
import com.car.vo.VehiclemotioncarVo;
/**
 * 顺风车
    * @ClassName: ProvincialController
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author 郝成浩
    * @date 2018年12月27日
    *
 */
@RestController
public class VehicleMotioncarController {
	
	@Autowired
	CitytableService citytableService;
	
	@Autowired
	ProvincialtableService provincialtableService;
	
	@Autowired
	CardetailstableService cardetailstableService;
	@Autowired
	VehiclemotioncarService vehiclemotioncarService;
	
	@Autowired
	StoretableService storetableService;
	
	Map<String, Object> maps = new HashMap<>();
	
	private static final Logger logger = LoggerFactory.getLogger(VehicleMotioncarController.class);
	
	@RequestMapping("/selevtBysIdToCitys")
	public List<Citytable> selevtBysIdToCity(@RequestParam("sId")String sId){
		return citytableService.selectAll(sId);
	}

	/**
	 * 添加
	    * @Title: addToVehi
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param vehi
	    * @param @return    参数
	    * @return Map<String,Object>    返回类型
	    * @throws
	 */
	@RequestMapping("/addsVehiasshunCar")
	public Map<String, Object> addToVehi(Vehiclemotioncar vehi){
		//查询该门店有没有车
		List<CardetailstableVo>list= cardetailstableService.selevtByCarId(vehi.getThecarstores(),vehi.getCarBrandId(), vehi.getCarModelId());
		if(list.size()==0){
			maps.put("msg", "123");
			return maps;
		}
	int random= Random.listRandom(list.size()-1);
	 int carId =list.get(random).getCarId();
	 vehi.setCarId(carId);
		int nums = vehiclemotioncarService.addVehicle(vehi);
		maps.put("msg", nums>0?true:false);
		return maps;
	}
	
	@RequestMapping("/selectToUpd")
	public Map<String, Object>selectToups(@RequestParam("id") Integer id){
		VehiclemotioncarVo veh= vehiclemotioncarService.selectById(id);
		for (int i = 0; i < 2; i++) {
			Citytable city= citytableService.selectByCityName(i>0?veh.getCarCity():veh.getTakeCarCity());
			Provincialtable pro= provincialtableService.selectByPrimaryKey(city.getProvinceid());
			maps.put("city"+i, city);
			maps.put("pro"+i, pro);
		}
	 Storetable sto=storetableService.selBystoreName(veh.getThecarstores());
	 maps.put("store", sto);
	 maps.put("veh", veh);
		return maps;	
	}
	
	@RequestMapping("/updToVehicle")
	public Map<String, Object> updToVehicle(Vehiclemotioncar vehi){
		logger.debug("执行修改updToVehicle方法!");
		maps.put("msg", vehiclemotioncarService.updVehicle(vehi)>0?true:false);
		return maps;
	}
	
	@RequestMapping("/delToVehicle")
	public Map<String, Object>delTovehicle(Integer id){
		logger.debug("执行删除delToVehicle方法!");
		maps.put("msg", vehiclemotioncarService.delVehicle(id)>0?true:false);
		return maps;
	}
	
	

}
